<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模版方法模式典型应用之自定义配置弹窗</title>
    <style>
        .modalBox {
            width: 400px;
            height: 400px;
            background-color: #ddd;
            border-radius: 10px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-top: -200px;
            margin-left: -200px;
        }
        .modalBox .modal-title {
            width: 100%;
            height: 100px;
            background-color: red;
            color: black;
            text-align: center;
            line-height: 100px;
        }
        .modalBox .modal-content {
            width: 100%;
            height: 200px;
            background-color: rgb(72, 158, 22);
            color: black;
            line-height: 200px;
            text-align: center;
        }
        .modalBox .modal-confirmBtn {
            position: absolute;
            bottom: 10px;
            right: 10px;
            width: 100px;
            height: 40px;
            border: 1px solid blue;
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            color: black;
            line-height: 40px;
        }
        .modalBox .modal-cancelBtn {
            position: absolute;
            bottom: 10px;
            right: 120px;
            width: 100px;
            height: 40px;
            border: 1px solid rgb(243, 64, 9);
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            line-height: 40px;
        }
        .modalBox .modal-cancelBtn:hover {
            background-color: rgb(243, 64, 9);
            color: white;
        }
        .modalBox .modal-confirmBtn:hover {
            background-color: blue;
            color: white;
        }
    </style>
</head>
<body>
    <script>
        // 弹窗父类
        class Modal {
            constructor(options={}) {
                this.title = options.title;
                this.content = options.content;
                this.confirmFn = options.confirmFn;
                this.cancelFn = options.cancelFn;
                // 初始化配置
                this.init();
                // c初始化事件
                this.eventListner();
            }
            init() {
                // 创建节点
                let modalBox = this.modalBox = document.createElement('div');
                modalBox.className = 'modalBox';
                document.body.appendChild(modalBox);
                // 弹窗标题
                let modalTitle = this.modalTitle = document.createElement('p');
                modalTitle.className = 'modal-title';
                modalTitle.innerHTML = this.title || '标题';
                modalBox.appendChild(modalTitle);
                // 弹窗内容
                let modalContent = this.modalContent = document.createElement('p');
                modalContent.className = 'modal-content';
                modalContent.innerHTML = this.content || '内容';
                modalBox.appendChild(modalContent);
                // 弹窗“确认”、“取消”按钮
                let confirmBtn = this.confirmBtn = document.createElement('span');
                confirmBtn.className = 'modal-confirmBtn';
                confirmBtn.innerHTML = 'ok';
                modalBox.appendChild(confirmBtn);

                let cancelBtn = this.cancelBtn =  document.createElement('span');
                cancelBtn.className = 'modal-cancelBtn';
                cancelBtn.innerHTML = 'cancel';
                modalBox.appendChild(cancelBtn);
            }
            eventListner() {
                this.modalBox.addEventListener('click', () => {
                    console.log('clickModalBox');
                });
                this.modalTitle.addEventListener('click', () => {
                    console.log('clickmodalTitle');
                });
                this.modalContent.addEventListener('click', () => {
                    console.log('clickmodalContent');
                });
                this.confirmBtn.addEventListener('click', () => {
                    console.log('clickconfirmBtn');
                    this.confirmFn();
                    this.hide();
                });this.cancelBtn.addEventListener('click', () => {
                    console.log('clickcancelBtn');
                    this.cancelFn();
                    this.hide();
                });
            }
            hide() {
                this.modalBox.style.display = 'none';
            };
            show() {
                this.modalBox.style.display = 'block';
            }
        }

        // 实例化一个弹窗
        // let testModal = new Modal({
        //     title:'我的标题',
        //     content:'我的内容',
        //     confirmFn: () => {
        //         console.log('自定义的回调函数---confrim')
        //     },
        //     cancelFn: () => {
        //         console.log('自定义的回调函数---cancel')
        //     }
        // });
        
        // 在父亲类的基础上去自定义弹窗
        class MyModal extends Modal {
            init() {
                super.init();
                // 删除内容节点
                this.modalContent.parentNode.removeChild(this.modalContent);
            }
        }

        // let testModal2 = new MyModal({
        //     title:'我的标题',
        //     confirmFn: () => {
        //         console.log('自定义的回调函数---confrim')
        //     },
        //     cancelFn: () => {
        //         console.log('自定义的回调函数---cancel')
        //     }
        // });

        // 在父亲类的基础上去自定义弹窗
        class XModal extends Modal {
            init() {
                super.init();
                // 增加关闭节点
                let closeBtn = this.closeBtn = document.createElement('span');
                closeBtn.style = 'width:50px;height:50px;position:absolute;top:10px;right:10px;background-color:#999;color:white;text-align:center;line-height:50px;cursor:pointer;border-radius:10px;';
                closeBtn.innerText = "X";
                this.modalBox.appendChild(closeBtn);
                
            }
            // 重写eventLinstner方法给关闭按钮加事件
            eventListner() {
                super.eventListner();
                this.closeBtn.addEventListener('click', () => {
                    this.hide();
                })
            }
        }
        let testModal3 = new XModal({
            title:'我的标题',
            contenty:'hello world',
            confirmFn: () => {
                console.log('自定义的回调函数---confrim')
            },
            cancelFn: () => {
                console.log('自定义的回调函数---cancel')
            }
        });
    </script>
    
</body>
</html>